//
// Created by YxMin on 2024/7/10.
//

#ifndef GAME_ABQUEUE_H
#define GAME_ABQUEUE_H
#include <queue>

template <class T>
class ABQueue {
private:
    std::queue<T> queues[2];
    int writingQueue = 0; //写A队列的同时只能读B队列。保证线程安全
public:
    void turn() {
        writingQueue ^= 1;
    }
    void enqueue(T const& element) {
        queues[writingQueue].push(element);
    }
    bool dequeue(T &save) {
        int readingQueue = writingQueue^1;
        if (queues[readingQueue].empty())
            return false;
        save = queues[readingQueue].front();
        queues[readingQueue].pop();
        return true;
    }
    void clear() {
        queues[0] = std::queue<T>();
        queues[1] = std::queue<T>();
    }
};


#endif //GAME_ABQUEUE_H
